package com.yycx.module.bus.provider.handler.haiying.order.search;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 导出物流对账表
 *
 * @author zyf
 */
@Component
public class HySearchExportListHandler implements PageInterceptor<HyOrderDetail>, ExportInterceptor {


    @Autowired
    private BusHyorderSendinfoService sendinfoService;

    @Override
    public void prepare(CriteriaQuery<HyOrderDetail> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate", DateUtils.format(DateUtils.getFirstDayOfMonth()));
        String endDate = params.get("endDate", DateUtils.format(DateUtils.getLastDayOfMonth()));
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        cq.addSelect(HyOrder.class, "orderId","saleManName", "outOrderNo", "sendDate", "customUserName", "customUserTel", "customAddress");
        cq.addSelect(HyOrderDetail.class, "drugsName", "drugsSpec", "saleNum", "printDrugJs");
        cq.addSelect(BusCustom.class, "customName");
        cq.addSelect(BusDept.class, "deptName");
        cq.eq("ho.orderState", CommonConstants.INT_2);
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "ho.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "ho.sendDate", endDate);
        cq.eq(true, "ho.companyId", companyId);
        cq.eq(true, "ho.sendState", CommonConstants.INT_1);
        cq.eq(HyOrder.class, "deptId");
        cq.createJoin(BusCustom.class);
        cq.createJoin(BusDept.class);
        cq.orderByAsc("ho.sendDate");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrderDetail> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String orderId = entityMap.get("orderId");
            BusHyorderSendinfo sendinfo = sendinfoService.findByOrderId(orderId);
            if (FlymeUtils.isNotEmpty(sendinfo)) {
                entityMap.put("fastMailNo", sendinfo.getFastMailNo());
                entityMap.put("fastMailName", sendinfo.getFastMailName());
                entityMap.put("invoiceNo", sendinfo.getInvoiceNo());
                entityMap.put("invoiceFastMailNo", sendinfo.getInvoiceFastMailNo());
                entityMap.put("invoiceFastMailName", sendinfo.getInvoiceFastMailName());
                entityMap.put("invoiceAddress", sendinfo.getInvoiceAddress());
                entityMap.put("invoiceTel", sendinfo.getInvoiceTel());
                entityMap.put("customFpId", sendinfo.getCustomFpId());
                entityMap.put("updateFastMailNo", sendinfo.getUpdateFastMailNo());
            }
        }
        return PageInterceptor.super.complete(cq, result, extra);
    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
    }

    @Override
    public TemplateExportParams initTemplateExportParams(Map params) {
        TemplateExportParams templateExportParams = new TemplateExportParams("excel/物流对账表2" + ".xlsx", true);
        return templateExportParams;
    }

    @Override
    public void initExportData(Map dataMap, List list,CriteriaQuery cq) {
        dataMap.put("month", DateUtils.getMonth());
        Long companyId = OpenHelper.getCompanyId();
        if (companyId.equals(5L)) {
            dataMap.put("companyName", "丹博瑞");
        }
        if (companyId.equals(1018L)) {
            dataMap.put("companyName", "赤壁医药");
        }
    }

}
